package com.zy.smartdb.controller;

import com.zy.smartdb.adapter.MyPreparedStatementAdapter;
import com.zy.smartdb.pojo.AjaxResult;
import com.zy.smartdb.pojo.FindParam;
import com.zy.smartdb.service.DbService;
import com.zy.smartdb.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Author: zara
 * @Version V3.0
 * @Date: 2022/9/21 2:48 下午
 */
@RestController
@RequestMapping("dml2")
@Slf4j
public class DmlController2 {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    DbService dbService;

    @PostMapping("/list")
    public AjaxResult list(@RequestBody FindParam findParam) throws Exception {
        findParam = (FindParam) StringUtil.safetyClone(findParam);
        MyPreparedStatementAdapter<List> adapter = new MyPreparedStatementAdapter(findParam, dbService.getTableInfo(findParam.getTable()));
        List result = jdbcTemplate.query(adapter, adapter, adapter);
        return AjaxResult.success(result);
    }

}

